import { defineStore } from 'pinia'
import { $get } from '@/lib/plugin/Aegis'

type RootState = {
  sideBarArticleList: {
    id: number;
    title: string;
    head_img: string;
    desc: string;
    tags: any;
    users: any;
    click: number;
    comment_total: number;
    star_total: number;
    created_at: string
    mockImgHeight: number;

  } | null;
  sideBarArticleOrderName: string | null;
  catalogList: string[] | null;
};

export const articleStore = defineStore('article', {
  // state 类似组件的data选项，函数形式返回对象
  state: () => {
    return {
      // 侧边栏文章列表
      sideBarArticleList: null,
      // 侧边栏文章类型
      sideBarArticleOrderName: 'updated_at',
      // 文章目录
      catalogList: []
    } as RootState
  },
  getters: {},
  actions: {
    changeSidebarArticleList (res:any):void {
      this.sideBarArticleList = res
    },
    changeCatalogList (res:any):void {
      this.catalogList = res
    },
    changeSideBarArticleOrderName (res:any):void {
      this.sideBarArticleOrderName = res
    },
    // 获取侧边栏文章列表
    async getSideBarArticleList (params:any) {
      const { nowPage, pageSize, orderName, orderBy } = params
      try {
        const data = await $get('article/list', {
          orderName,
          orderBy,
          nowPage,
          pageSize
        })
        this.sideBarArticleOrderName = orderName
        this.sideBarArticleList = data.rows
      } catch (error) {
        // console.log(error)
      }
    }
  }
})
